<?php 
class ControllerAccountWishList extends FrontController {
	public function index() {
    	if (!$this->customer->isLogged()) {
	  		$this->session->data['redirect'] = US('account/wishlist');
	  		$this->redirect(US('account/login')); 
    	}    	
		
		M('catalog/product');

		if (!isset($this->session->data['wishlist'])) {
			$this->session->data['wishlist'] = array();
		}
		
		if (isset($this->request->get['remove'])) {
			$key = array_search($this->request->get['remove'], $this->session->data['wishlist']);
			
			if ($key !== false) {
				unset($this->session->data['wishlist'][$key]);
			}
		
			$this->session->set_flashdata('success', $this->_['text_remove']);
			$this->redirect(U('account/wishlist'));
		}
						
		$this->document->setTitle($this->_['heading_title']);	
      	
      	$this->data['breadcrumbs'][] = array(       	
        	'text'      => $this->_['text_account'],
			'href'      => U('account/account', '', 'SSL'),
        	'separator' => $this->_['text_separator']
      	);

      	$this->data['breadcrumbs'][] = array(       	
        	'text'      => $this->_['heading_title'],
			'href'      => U('account/wishlist'),
        	'separator' => $this->_['text_separator']
      	);
								
		$this->data['success'] = $this->session->flashdata('success');
		
		$this->data['products'] = array();
	
		foreach ($this->session->data['wishlist'] as $key => $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);
			
			if ($product_info) { 

				if ($product_info['quantity'] <= 0) {
					$stock = $product_info['stock_status'];
				} elseif (C('config_stock_display')) {
					$stock = $product_info['quantity'];
				} else {
					$stock = $this->_['text_instock'];
				}
							
				$this->data['products'][] = array(
					'product_id' => $product_info['product_id'],
					'thumb'      => thumb($product_info['image'], C('config_image_wishlist_width'), C('config_image_wishlist_height')),
					'name'       => $product_info['name'],
					'model'      => $product_info['model'],
					'stock'      => $stock,
					'price'       => pprintf($product_info['price'], $product_info['tax_class_id']),
					'special'     => pprintf($product_info['special'], $product_info['tax_class_id']),
					'href'       => $product_info['link'],
					'remove'     => U('account/wishlist', 'remove=' . $product_info['product_id'])
				);
			} else {
				unset($this->session->data['wishlist'][$key]);
			}
		}	

		$this->display('account/wishlist.tpl');		
	}
	
	public function add() {
		$json = array();

		if (!isset($this->session->data['wishlist'])) {
			$this->session->data['wishlist'] = array();
		}
				
		if (isset($this->request->post['product_id'])) {
			$product_id = $this->request->post['product_id'];
		} else {
			$product_id = 0;
		}
		
		M('catalog/product');
		
		$product_info = $this->model_catalog_product->getProduct($product_id);
		
		if ($product_info) {
			if (!in_array($this->request->post['product_id'], $this->session->data['wishlist'])) {	
				$this->session->data['wishlist'][] = $this->request->post['product_id'];
			}
			 
			if ($this->customer->isLogged()) {			
				$json['success'] = sprintf($this->_['text_success'], $product_info['link'], v5_substr($product_info['name'], 60), U('account/wishlist'));				
			} else {
				$json['success'] = sprintf($this->_['text_login'], US('account/login'), US('account/register'), $product_info['link'], v5_substr($product_info['name'],60), U('account/wishlist'));				
			}
			
			$json['total'] = sprintf($this->_['text_wishlist'], (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
		}	
		
		$this->response->setOutput(json_encode($json));
	}	
}
?>